{extend name="main" /}

{block name="body"}
{include file="breadcrumb" /}
<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-card-header layuiadmin-card-header-auto">
            <form class="layui-form" method="get">
                <input type="hidden" name="bcid" value="{:input('bcid')}"><!--保留当前位置的bcid参数-->
                <div class="layui-form-item">
                    <div class="layui-btn-group">
                        <a href="{:url('add')}" data-width="800px" data-height="600px" class="layui-btn xn_open" title="添加">
                            添加
                        </a>
                        <button type="button" class="layui-btn layui-btn-normal" id="file">导入</button>
                        <input class="layui-upload-file" type="file" accept="" name="file">
                        <a href="javascript:;" title="确认要删除吗？" class="layui-btn layui-btn-danger" id="deleteAll">
                            删除
                        </a>
                        <button type="button" class="layui-btn layui-btn-warm" onclick="matching_company()">匹配公司</button>
                        <button type="button" class="layui-btn layui-btn-warm" onclick="get_shop_num()">获取店铺数</button>
                    </div>
                </div>
                <div class="layui-form-item">
                    <div class="layui-inline">
                        <label class="layui-form-label" style="text-align: left;">总计：{$count}</label>
                        <div class="layui-input-inline">
                            <select name="parent_typeid" lay-filter="parent_typeid">
                                <option value="0">请选择</option>
                                {foreach name="parent_typelist" item="vo"}
                                <option value="{$vo.id}" {if $param.parent_typeid==$vo.id}selected{/if}>{$vo.title}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <select name="typeid" id="typeid">
                                <option value="0">请选择</option>
                                {foreach name="typelist" item="vo"}
                                <option value="{$vo.id}" {if $param.typeid==$vo.id}selected{/if}>{$vo.title}</option>
                                {/foreach}
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline" style="width: 70px;">
                            <select name="tort"">
                                <option value="0">默认</option>
                                <option value="1" {if $param.tort=='1'}selected{/if}>侵权</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="layui-input-inline">
                            <input type="text" name="kw" class="layui-input" value="{$param.kw}">
                        </div>
                        <div class="layui-input-inline" style="width: 70px;">
                            <select name="field">
                                <option value="title">名称</option>
                                <option value="alias" {if $param.field=='alias'}selected{/if}>别名</option>
                                <option value="company" {if $param.field=='company'}selected{/if}>公司</option>
                            </select>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button class="layui-btn">
                            <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                        </button>
                    </div>
                </div>
            </form>
        </div>
        <div class="layui-card-body">
            {foreach name="repeat" item="vo"}
            <blockquote class="layui-elem-quote">
                提示：ID:{$vo.proid} [{$vo.proid|get_repeat_name}] 与 ID:{$vo.repid} [{$vo.repid|get_repeat_name}] 的别称有重复，请查看！
                <a href="javascript:;" data-id="{$vo.proid}"  onclick="delRepeat(this)" class="layui-btn layui-btn-danger layui-btn-xs">移除提示</a>
            </blockquote>
            {/foreach}
            <table class="layui-table">
                <tr>
                    <th><b><input type="checkbox" id="selectAll"></b></th>
                    <th><b>ID</b></th>
                    <th><b>大类</b></th>
                    <th><b>小类</b></th>
                    <th><b>项目名称</b></th>
                    <th><b>项目别名</b></th>
                    <th><b>公司名称</b></th>
                    <th><b>存在公司</b></th>
                    <th><b>门店</b></th>
                    <th><b>来源</b></th>
                    <th><b>操作</b></th>
                </tr>

                <tbody>
                {foreach name="list" item="vo"}
                <tr>
                    <td><input type="checkbox" name="ids[]" value="{$vo.id}" class="checkids"></td>
                    <td>{$vo.id}</td>
                    <td>{$vo.parent_typeid|get_typename}</td>
                    <td>{$vo.typeid|get_typename}</td>
                    <td>{if $vo.tort==1}<span style="color: red;">{$vo.title}</span>{else}{$vo.title}{/if}</td>
                    <td>{$vo.alias}</td>
                    <td>{$vo.company}</td>
                    <td>{if $vo.company_exist}存在{else}不存在{/if}</td>
                    <td>{if $vo.shop_get}{$vo.shop_num}{else}未执行{/if}</td>
                    <td>{$vo.source}</td>
                    <td>
                        <a href="{:url('edit',array('id'=>$vo['id']))}" data-width="800px" data-height="600px" title="编辑" class="layui-btn layui-btn-normal layui-btn-xs xn_open">
                            <i class="layui-icon layui-icon-edit"></i>编辑
                        </a>
                        <a href="{:Url('delete',array('id'=>$vo['id']))}" title="确认要删除吗？" class="layui-btn layui-btn-danger layui-btn-xs xn_delete">
                            <i class="layui-icon layui-icon-delete"></i>删除
                        </a>
                    </td>
                </tr>
                {/foreach}
                </tbody>
            </table>
        </div>
    </div>
    <div class="pages">
        {$list|raw}
    </div>
</div>
{/block}

{block name="js"}
<script>
    var upload = layui.upload;
    upload.render({
        elem: '#file'
        ,url: "{:url('import')}" //改成您自己的上传接口
        ,accept: 'file' //普通文件
        ,exts: 'xlsx|xls' 
        ,done: function(res){
            layer.msg(res.msg,{time:2000},function(){
                window.location.reload();
            });
        }
        ,error: function(){
            layer.msg('请求失败');
        }
    });

    $(function() {
        var $selectAll = $("#selectAll");
        $selectAll.click(function() {
            if ($selectAll.prop("checked") == true) {
                $('.checkids').prop("checked", true);
            } else {
                $('.checkids').prop("checked", false);
            }
        });

        $("#deleteAll").click(function(){
            layer.confirm('确认删除选定内容吗?', {icon: 3, title:'操作提示'}, function(index){
                var ids = [];
                $('.checkids:checked').each(function(index,obj){
                    ids.push(obj.value);
                });
                $.get("{:Url('deleteAll')}?ids="+ids,function(data){
                    layer.msg(data.msg, {
                    time: 1000 
                    }, function(){
                    location.reload();
                    }); 
                });
            });
        });
    });

    var form = layui.form;
    form.on('select(parent_typeid)', function(res) { 
        $("#typeid").empty();
        if(res.value != 0){
            $.get("{:url('admin/DataType/getSonType')}?pid="+res.value,function(result){
                if(result.status){
                    $("#typeid").append(new Option('请选择',0));
                    $.each(result.data,function(index,item){
                        $("#typeid").append(new Option(item.title,item.id));
                    });
                    form.render("select");
                }
            });
        }else{
            $("#typeid").append(new Option('请选择',0));
            form.render("select");
        }
    })

    function delRepeat(obj){
        $.get("{:Url('delRepeat')}?id="+$(obj).attr('data-id'),function(result){
            $(obj).parent().remove();
        });
    }

    function matching_company(start=0,end=0,flag=0){
        if(start==0){
            layer.msg('开始执行', {icon: 1,time: 0});
        }
        $.get("{:url('matchingCompany')}",{start:start,end:end,flag:flag},function(res){
            if(res.status){
                layer.msg(res.msg, {icon: 1,time: 0}); 
                matching_company(res.data.start,res.data.end,flag);
            }else{
                layer.msg(res.msg, {icon: 1,time: 2000},function(){
                    location.reload();
                });
            }
        });
    }

    function get_shop_num(start=0,end=0,flag=0){
        if(start==0){
            layer.msg('开始执行', {icon: 1,time: 0});
        }
        $.get("{:url('getShopNum')}",{start:start,end:end,flag:flag},function(res){
            if(res.status){
                layer.msg(res.msg, {icon: 1,time: 0}); 
                get_shop_num(res.data.start,res.data.end,flag);
            }else{
                layer.msg(res.msg, {icon: 1,time: 2000},function(){
                    location.reload();
                });
            }
        });
    }
</script>
{/block}